﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaoJianSoft.Common;
using System.Globalization;
using System.Xml.Serialization;

namespace BaoJianSoft.ExtensionModel.Statistic
{
	/// <summary>
	/// Statistic definition.
	/// </summary>
	public partial class StatisticDefinition
	{
		/// <summary>
		/// Custom assigned id which won't be serialized.
		/// </summary>
		[XmlIgnore]
		public Guid Id { get; set; }

		/// <summary>
		/// Returns the name of statistic definition.
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return this.Name;
		}
	}

	/// <summary>
	/// Abstract filter algorithm indicates how to filter and analyze data by statistic configuration.
	/// </summary>
	public partial class StatisticFilter : IStatisticFilter
	{
		/// <summary>
		/// Does the value match the algorithm.
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public abstract bool IsMatch(object value);

		/// <summary>
		/// The meaningful description generated from the statistic filter properties. 
		/// </summary>
		[XmlIgnore]
		public string Description
		{
			get { return this.ToString(); }
		}
	}

	/// <summary>
	/// The partial class used to provide functionalities to get all items.
	/// </summary>
	public partial class CandidateStatisticPropertyDataSource
	{
		/// <summary>
		/// Get all items.
		/// </summary>
		/// <returns></returns>
		public IEnumerable<StatisticItem> GetItems()
		{
			if (this is CandidateStatisticPropertyCallbackDataSource)
			{
				CandidateStatisticPropertyCallbackDataSource callbackDataSource = this as CandidateStatisticPropertyCallbackDataSource;
				Type callbackType = Kit.GetType(callbackDataSource.Type);
				ICandidateStatisticPropertyItemResolver resolver = Activator.CreateInstance(callbackType) as ICandidateStatisticPropertyItemResolver;
				return resolver.Resolve();
			}
			else if (this is CandidateStatisticPropertyStaticDataSource)
			{
				CandidateStatisticPropertyStaticDataSource statisticDataSource = this as CandidateStatisticPropertyStaticDataSource;
				return statisticDataSource.Item;
			}

			throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Not supported candidate statistic property data source \"{0}\".", this));
		}
	}

	/// <summary>
	/// 候选统计字段
	/// </summary>
	public partial class CandidateStatisticProperty : ICloneable
	{
		#region ICloneable Members

		object ICloneable.Clone()
		{
			return this.Clone();
		}

		#endregion

		/// <summary>
		/// Get deep memory copy of this instance.
		/// </summary>
		/// <returns></returns>
		public CandidateStatisticProperty Clone()
		{
			return new CandidateStatisticProperty
			{
				Algorithm = this.Algorithm,
				Item = this.Item,
				PropertyLabel = this.PropertyLabel,
				PropertyName = this.PropertyName,
				PropertyType = this.PropertyType
			};
		}	
	}
}
